# API测试脚本
# 测试员工管理API的所有端点

Write-Host "开始测试员工管理API..." -ForegroundColor Green

# 基础URL
$baseUrl = "http://localhost:8080/api/employees"

# 测试数据
$employee = @{
    name = "张三"
    gender = "男"
    age = 25
    phone = "13800138001"
    email = "zhangsan@example.com"
    hireDate = "2024-06-28"
    position = "前台接待"
    roleId = 1
    status = "在职"
} | ConvertTo-Json

Write-Host "测试数据: $employee" -ForegroundColor Yellow

# 1. 测试创建员工
Write-Host "`n1. 测试创建员工 (POST /api/employees)" -ForegroundColor Cyan
try {
    $response = Invoke-RestMethod -Uri $baseUrl -Method POST -Body $employee -ContentType "application/json"
    Write-Host "创建员工成功: $($response | ConvertTo-Json)" -ForegroundColor Green
    $employeeId = $response.data.id
} catch {
    Write-Host "创建员工失败: $($_.Exception.Message)" -ForegroundColor Red
    $employeeId = 1  # 使用默认ID继续测试
}

# 2. 测试根据ID查询员工
Write-Host "`n2. 测试根据ID查询员工 (GET /api/employees/$employeeId)" -ForegroundColor Cyan
try {
    $response = Invoke-RestMethod -Uri "$baseUrl/$employeeId" -Method GET
    Write-Host "查询员工成功: $($response | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "查询员工失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 3. 测试分页查询员工
Write-Host "`n3. 测试分页查询员工 (GET /api/employees)" -ForegroundColor Cyan
try {
    $response = Invoke-RestMethod -Uri "$baseUrl?pageNum=1&pageSize=10" -Method GET
    Write-Host "分页查询成功: $($response | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "分页查询失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 4. 测试条件查询员工
Write-Host "`n4. 测试条件查询员工 (GET /api/employees/search)" -ForegroundColor Cyan
try {
    $response = Invoke-RestMethod -Uri "$baseUrl/search?name=张三&pageNum=1&pageSize=10" -Method GET
    Write-Host "条件查询成功: $($response | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "条件查询失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 5. 测试更新员工
Write-Host "`n5. 测试更新员工 (PUT /api/employees/$employeeId)" -ForegroundColor Cyan
$updateEmployee = @{
    name = "李四"
    gender = "男"
    age = 26
    phone = "13800138002"
    email = "lisi@example.com"
    hireDate = "2024-06-28"
    position = "客房服务"
    roleId = 1
    status = "在职"
} | ConvertTo-Json

try {
    $response = Invoke-RestMethod -Uri "$baseUrl/$employeeId" -Method PUT -Body $updateEmployee -ContentType "application/json"
    Write-Host "更新员工成功: $($response | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "更新员工失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 6. 测试批量创建员工
Write-Host "`n6. 测试批量创建员工 (POST /api/employees/batch)" -ForegroundColor Cyan
$batchEmployees = @(
    @{
        name = "王五"
        gender = "男"
        age = 28
        phone = "13800138003"
        email = "wangwu@example.com"
        hireDate = "2024-06-28"
        position = "保安"
        roleId = 1
        status = "在职"
    },
    @{
        name = "赵六"
        gender = "女"
        age = 24
        phone = "13800138004"
        email = "zhaoliu@example.com"
        hireDate = "2024-06-28"
        position = "清洁员"
        roleId = 1
        status = "在职"
    }
) | ConvertTo-Json

try {
    $response = Invoke-RestMethod -Uri "$baseUrl/batch" -Method POST -Body $batchEmployees -ContentType "application/json"
    Write-Host "批量创建成功: $($response | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "批量创建失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 7. 测试批量删除员工
Write-Host "`n7. 测试批量删除员工 (DELETE /api/employees/batch)" -ForegroundColor Cyan
$deleteIds = @(2, 3) | ConvertTo-Json

try {
    $response = Invoke-RestMethod -Uri "$baseUrl/batch" -Method DELETE -Body $deleteIds -ContentType "application/json"
    Write-Host "批量删除成功: $($response | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "批量删除失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 8. 测试删除员工
Write-Host "`n8. 测试删除员工 (DELETE /api/employees/$employeeId)" -ForegroundColor Cyan
try {
    $response = Invoke-RestMethod -Uri "$baseUrl/$employeeId" -Method DELETE
    Write-Host "删除员工成功: $($response | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "删除员工失败: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`nAPI测试完成!" -ForegroundColor Green
